package org.dromara.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.cms.domain.CmsCategory;
import org.dromara.cms.domain.bo.CmsCategoryBo;
import org.dromara.cms.domain.vo.CmsCategoryVo;
import org.dromara.cms.mapper.CmsCategoryMapper;
import org.dromara.cms.service.ICmsCategoryService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * CMS分类Service业务层处理
 *
 * @author Spring
 * @date 2024-12-24
 */
@RequiredArgsConstructor
@Service
public class CmsCategoryServiceImpl implements ICmsCategoryService {

    private final CmsCategoryMapper baseMapper;

    /**
     * 查询CMS分类
     *
     * @param id 主键
     * @return CMS分类
     */
    @Override
    public CmsCategoryVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询CMS分类列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return CMS分类分页列表
     */
    @Override
    public TableDataInfo<CmsCategoryVo> queryPageList(CmsCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmsCategory> lqw = buildQueryWrapper(bo);
        Page<CmsCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的CMS分类列表
     *
     * @param bo 查询条件
     * @return CMS分类列表
     */
    @Override
    public List<CmsCategoryVo> queryList(CmsCategoryBo bo) {
        LambdaQueryWrapper<CmsCategory> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc(CmsCategory::getSort);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmsCategory> buildQueryWrapper(CmsCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsCategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), CmsCategory::getTitle, bo.getTitle());
        lqw.eq(bo.getParentId() != null, CmsCategory::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getAncestors()), CmsCategory::getAncestors, bo.getAncestors());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), CmsCategory::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getCategoryType()), CmsCategory::getCategoryType, bo.getCategoryType());
        lqw.eq(StringUtils.isNotBlank(bo.getTemplate()), CmsCategory::getTemplate, bo.getTemplate());
        lqw.eq(bo.getSort() != null, CmsCategory::getSort, bo.getSort());
        lqw.eq(bo.getIsShow() != null, CmsCategory::getIsShow, bo.getIsShow());
        lqw.eq(StringUtils.isNotBlank(bo.getSeoTitle()), CmsCategory::getSeoTitle, bo.getSeoTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getSeoKeywords()), CmsCategory::getSeoKeywords, bo.getSeoKeywords());
        lqw.eq(StringUtils.isNotBlank(bo.getSeoDescription()), CmsCategory::getSeoDescription, bo.getSeoDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomFields()), CmsCategory::getCustomFields, bo.getCustomFields());
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), CmsCategory::getCategoryName, bo.getCategoryName());

        return lqw;
    }

    /**
     * 新增CMS分类
     *
     * @param bo CMS分类
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CmsCategoryBo bo) {
        CmsCategory add = MapstructUtils.convert(bo, CmsCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改CMS分类
     *
     * @param bo CMS分类
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CmsCategoryBo bo) {
        CmsCategory update = MapstructUtils.convert(bo, CmsCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmsCategory entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除CMS分类信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
